package com.advaita.gui;

import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.advaita.license.Customer;

public class GoodKey extends JFrame implements ActionListener {
Customer customer;
	// JButton runPathwayBtn = new JButton("Run Pathway Guide");
	private JButton runPathwayBtn;

	public GoodKey(Customer lCustomer) {
		super("Sucessful Activation");

		customer = new Customer();
		customer = lCustomer;
		/**********************************/
		// image 1
		String property = "swing.aatext";
		if (null == System.getProperty(property))
			System.setProperty(property, "true");

		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 2;
		gbc.gridy = 0;
		gbc.gridwidth = 4;
		gbc.gridheight = 3;
		gbc.anchor = GridBagConstraints.LINE_END;

		// natural height, maximum width
		gbc.fill = GridBagConstraints.BOTH;

		gbc.ipadx = 40;
		gbc.ipady = 40;
		gbc.gridwidth = 5;
		gbc.gridheight = 5;

		String path = "images/advaita.png";
		File file = new File(path);
		BufferedImage image;
		try {
			image = ImageIO.read(file);
			JLabel label = new JLabel(new ImageIcon(image));
			panel.add(label, gbc);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/**********************************/
		// text
		gbc.ipadx = 3;
		gbc.ipady = 10;
		gbc.gridx = 0;
		gbc.gridy = 5;
		
		// Need to change this for a pc 
		JLabel label = new JLabel(
				"  License activated and stored in file: /Library/Frameworks/Advaita/license.xml");

		label.setFont(new Font("serif", Font.PLAIN, 16));

		panel.add(label, gbc);

		/**********************************/
		// image 2
		gbc.gridx = 1;
		gbc.gridy = 11;
		gbc.gridwidth = 3;
		gbc.gridheight = 1;

		gbc.anchor = GridBagConstraints.LINE_START;
		String path2 = "images/unlockKeySmall.png";
		File file2 = new File(path2);
		BufferedImage image2;
		try {
			image2 = ImageIO.read(file2);
			JLabel label1 = new JLabel(new ImageIcon(image2));

			// PJM use the screen size for the position
			// label.setBounds(2, 1, 500, 280);

			panel.add(label1, gbc);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/**********************************/
		// btn

		// OK, I don't know if you will like how this works.
		// The text disappears in the box when a key is entered.
		runPathwayBtn = new JButton("Run Pathway Guide");
		runPathwayBtn.addActionListener(this);
		runPathwayBtn.setActionCommand("Open");

		gbc.gridx = 4;
		gbc.gridy = 11;

		gbc.ipadx = 1;
		gbc.ipady = 1;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridwidth = 2;
		gbc.gridheight = 3;

		panel.add(runPathwayBtn, gbc);

		getContentPane().add(panel);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);

	} // end of constructor GoodKey

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == runPathwayBtn) {

			// Collect all active frames in a array
			Frame[] activeframes = Frame.getFrames();
		
			for (int i = 0; i < activeframes.length; i++) {
				// Get frame's title
				String title = activeframes[i].getTitle();
				activeframes[i].dispose();

				boolean isVisible = activeframes[i].isVisible();
			}
			PathwayGuide pathwayguide = new PathwayGuide(customer);

		}
	}
}
